package org.example.netty.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class MyEncoder extends MessageToByteEncoder<MyMessage> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, MyMessage myMessage, ByteBuf byteBuf) throws Exception {
        byte[] contentBytes = myMessage.getContent().getBytes();
        byteBuf.writeInt(contentBytes.length);  //在当前writeIndex处写入一个int值，并将writeIndex增加4（因为int值占用4字节）
        byteBuf.writeBytes(contentBytes);
    }
}
